package com.matt.service.cookbook.impl;

import com.matt.domain.cookbook.Recipe;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.cookbook.RecipeNativeRepository;
import com.matt.repository.cookbook.RecipeRepository;
import com.matt.service.cookbook.RecipeService;
import com.matt.service.cookbook.RecipeShowService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RecipeServiceImpl implements RecipeService {

	@Autowired
	private RecipeRepository recipeRepository;

	@Autowired
	private RecipeNativeRepository recipeNativeRepository;

	@Autowired
	private RecipeShowService recipeShowService;

	public Recipe insertSelective(Recipe recipe){
		Recipe insertRecipe = recipeRepository.save(recipe);
		return insertRecipe;
	}

	@Override
	public Recipe findByIdAndAreaId(Long id, String areaId) {
		return recipeRepository.findByAreaIdAndId(areaId, id);
	}

	@Override
	public List<Map<String, Object>> selectRecipeFoodByTime(Map<String, Object> params) {
		return recipeNativeRepository.selectRecipeFoodByTime(params);
	}

	@Override
	public List<Recipe> selectRecipeListByCondition(Map<String, Object> filter) {
		List<Recipe> list = recipeNativeRepository.selectRecipeListByCondition(filter);
		return list;
	}

	@Override
	public String selectEarlyRecipeDateBySchoolId(String areaId) {
		return recipeNativeRepository.selectEarlyRecipeDateByAreaId(areaId);
	}

	@Override
	@Transactional
	public int insertRecipeByRecipeList(List<Recipe> recipeList) {
		List<Recipe> result = recipeRepository.saveAll(recipeList);
		int count = result==null ? 0 : result.size();
		return count;
	}

//	@Override
//	@Transactional
//	public int insertRecipeByRecipeList(List<Recipe> recipeList) {
//
//		for (Recipe recipe : recipeList){
//			recipeRepository.deleteBy(recipe.getFoodId(),recipe.getRecipeTypeId(),
//					new DateTime(recipe.getRecipeDate()).toString("yyyy-MM-dd"),recipe.getAreaId());
//		}
//
//		List<Recipe> result = recipeRepository.save(recipeList);
//
//		int count = result==null ? 0 : result.size();
//		return count;
//	}

	@Override
	@Transactional
	public int insertRecipeJSON(List<DateTime> recipeDateList,String areaId) {
		for (DateTime recipeDate : recipeDateList){
			String recipeDateStr = recipeDate.toString("yyyy-MM-dd");
			recipeShowService.deleteRecipeShowByTimeAndAreaId(recipeDateStr, areaId);
			String recipeShowJson = recipeShowService.createRecipeShowJson(recipeDateStr, areaId);
			recipeShowService.createRecipeShow(areaId, recipeDate.toDate(), recipeShowJson);
		}

		return recipeDateList.size();
	}

	@Override
	public List<String> selectSchoolIdByTime(String startTime, String endTime) {
		return recipeNativeRepository.selectAreaIdsByTime(startTime, endTime);

	}

	@Override
	public void deleteRecipeBySchoolAndTime(String areaId, String timeStr, Long recipeTypeId) {
		Date time = DateUtil.parseToDate(timeStr);
		recipeRepository.deleteByAreaIdAndRecipeTypeIdAndRecipeDateGreaterThan(areaId, recipeTypeId, time);
	}

	@Override
	public void deleteByFoodIdAndRecipeTypeIdAndRecipeDateAndAreaId(long foodId, long recipeTypeId, String recipeDate, String areaId) {
		recipeRepository.deleteBy(foodId,recipeTypeId,recipeDate,areaId);
	}

	@Override
	public void deleteById(Long id) {
		recipeRepository.deleteById(id);
	}

	@Override
	public void deleteAll(List<Recipe> recipes){
		recipeRepository.deleteAll(recipes);
	}

	@Override
	public List<Map<String, Object>> selectRecipeFoodByFilter(Map<String, Object> params) {
		return recipeNativeRepository.selectRecipeFoodByFilter(params);
	}

	@Override
	public List<Map<String, Object>> selectRecipeTypeIdByTimeAndAreaId(String recipeDate, String areaId) {
		return recipeNativeRepository.selectRecipeTypeIdByTimeAndAreaId(recipeDate, areaId);
	}

	@Override
	public List<Recipe> selectList(String startDateStr, String endDateStr, String areaId) {
		Date startDate = DateUtil.parseToDate(startDateStr);
		Date endDate = DateUtil.parseToDate(endDateStr);
		return recipeRepository.findAllByAreaIdAndRecipeDateBetweenOrderById(areaId, startDate, endDate);
	}
}
